package com.hu.zhcc.shiro.service.xsf.impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.xsf.TDiseaseLibraryMapper;
import com.hu.zhcc.shiro.entity.xsf.dto.TDiseaseLibraryDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDiseaseLibrary;
import com.hu.zhcc.shiro.service.xsf.TDiseaseLibraryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TDiseaseLibraryImpl implements TDiseaseLibraryService {
    @Autowired
    private TDiseaseLibraryMapper tDiseaseLibraryMapper;

    @Override
    public Page<TDiseaseLibraryDTO> queryAllTDisease(Map<String, Object> paremeters, int offset, int limit) {
        int total = this.tDiseaseLibraryMapper.countTDiseaseLibrary(paremeters);
        List<TDiseaseLibraryDTO> dtoList = null;
        if(total > 0) {
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TDiseaseLibrary> list = this.tDiseaseLibraryMapper.queryAllTDisease(pageRequest);
            dtoList = new ArrayList<TDiseaseLibraryDTO>(list.size());
            for(TDiseaseLibrary tDiseaseLibrary : list) {
                TDiseaseLibraryDTO dto = new TDiseaseLibraryDTO();
                BeanUtils.copyProperties(tDiseaseLibrary, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TDiseaseLibraryDTO>(total, dtoList);
    }

    @Override
    public TDiseaseLibraryDTO getById(int id) {
        TDiseaseLibrary tDiseaseLibrary = this.tDiseaseLibraryMapper.selectByPrimaryKey(id);
        if(tDiseaseLibrary == null) {
            return null;
        }
        TDiseaseLibraryDTO dto = new TDiseaseLibraryDTO();
        BeanUtils.copyProperties(tDiseaseLibrary, dto);
        return dto;
    }

    @Override
    public List<TDiseaseLibrary> queryAllTDisease1() {
        return tDiseaseLibraryMapper.queryAllTDisease1();
    }

    @Override
    public TDiseaseLibrary selectByPrimaryKey(Integer id) {
        return tDiseaseLibraryMapper.selectByPrimaryKey(id);
    }

    @Override
    public TDiseaseLibraryDTO saveTDiseaseLibrary(TDiseaseLibraryDTO dto) {
        TDiseaseLibrary tDiseaseLibrary = new TDiseaseLibrary();
        BeanUtils.copyProperties(dto, tDiseaseLibrary);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tDiseaseLibrary.setCreateTime(simpleDateFormat.format(new Date()));
        this.tDiseaseLibraryMapper.insertSelective(tDiseaseLibrary);
        dto.setId(tDiseaseLibrary.getId());
        return dto;
    }

    @Override
    public int removeTDiseaseLibrary(int id) {
        return tDiseaseLibraryMapper.deleteByPrimaryKey(id);

    }

    @Override
    public int updateTDiseaseLibrary(TDiseaseLibrary tDiseaseLibrary) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        tDiseaseLibrary.setUpdateTime(simpleDateFormat.format(new Date()));
        return tDiseaseLibraryMapper.updateByPrimaryKeySelective(tDiseaseLibrary);

    }
}
